#include "creategroupwindow.h"
#include "ui_creategroupwindow.h"
#include "mytcpsocket.h"
#include <QFile>
#include <QFileDialog>


CreateGroupWindow::CreateGroupWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CreateGroupWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("创建群聊");
    // 窗口渲染
    setQssStyle(QString("../style/CreateGroupStyle.qss"),this);
    // 无边框
    this->setWindowFlags(Qt::FramelessWindowHint);
    // 初始化tablewidget
    initTableWidget();
}

CreateGroupWindow::~CreateGroupWindow()
{
    delete ui;
}

void CreateGroupWindow::initTableWidget()
{
    // 设置行列数:2x2
    ui->tableWidget->setRowCount(2);
    ui->tableWidget->setColumnCount(2);

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//根据表格自适应列宽的大小
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);//根据表格自适应列高的大小
    ui->tableWidget->verticalHeader()->setHidden(true); //隐藏行标题
    ui->tableWidget->horizontalHeader()->setHidden(true);//隐藏列标题


    QTableWidgetItem* item1 = new QTableWidgetItem();
    QPixmap tmp = QPixmap(":/prefix2/software/addother.png");
    item1->setIcon(QIcon(tmp));
    item1->setTextAlignment(Qt::AlignCenter); // 可选：设置文本对齐方式
    item1->setData(Qt::UserRole,QVariant("addother.png"));

    QTableWidgetItem* item2 = new QTableWidgetItem();
    QPixmap tmp2 = PixmapToRoundOpenCV(QPixmap(":/prefix2/software/pencil.png"));
    item2->setIcon(QIcon(tmp2));
    item2->setTextAlignment(Qt::AlignCenter); // 可选：设置文本对齐方式
    item2->setData(Qt::UserRole,QVariant("pencil.png"));

    QTableWidgetItem* item3 = new QTableWidgetItem();
    QPixmap tmp3 = PixmapToRoundOpenCV(QPixmap(":/prefix2/software/heart.png"));
    item3->setIcon(QIcon(tmp3));
    item3->setTextAlignment(Qt::AlignCenter); // 可选：设置文本对齐方式
    item3->setData(Qt::UserRole,QVariant("heart.png"));

    QTableWidgetItem* item4 = new QTableWidgetItem();
    QPixmap tmp4 = PixmapToRoundOpenCV(QPixmap(":/prefix2/software/earth.png"));
    item4->setIcon(QIcon(tmp4));
    item4->setTextAlignment(Qt::AlignCenter); // 可选：设置文本对齐方式
    item4->setData(Qt::UserRole,QVariant("earth.png"));

    // 设置默认群头像
    ui->tableWidget->setIconSize(QSize(40,40));
    ui->tableWidget->setItem(0,0,item1);
    ui->tableWidget->setItem(0,1,item2);
    ui->tableWidget->setItem(1,0,item3);
    ui->tableWidget->setItem(1,1,item4);

    giconUrl = "earth.png";
    defaultIcon = "1";

}
// 创建群聊
void CreateGroupWindow::on_btn_create_clicked()
{
    // 获取用户输入的群信息
    QString gname = ui->le_gname->text();
    QString filename = giconUrl.split('/').back();
    QString introduce = ui->te_introduce->toPlainText();
    // 封装创建群聊请求
    QVariantMap map;
    map["cmd"]="create_group";
    map["gname"]=gname;
    map["gicon"]=filename;
    map["gboss"]=GlobalObject::getGlobalObject()->getUserInfo().sdata.dd_num;
    if(ui->radioButton->isChecked())
        map["told"]="1";
    else
        map["told"]="0";
    map["defaulticon"]=defaultIcon;
    map["gintroduce"] = introduce;
    // 如果没有选择默认群图标，那么需要将此图标上传到服务器,服务器将其改名为群ID
    if(defaultIcon == "0"){
        QString srvpath = QString("%1:/DragonIM/img/%2").arg(SRV_HOSTNAME).arg(filename);
        GlobalObject::getGlobalObject()->uploadToServer_Small(giconUrl,srvpath);
    }
    // 发送创建群聊请求
    GlobalObject::getGlobalObject()->sendRequest(map);

}


void CreateGroupWindow::on_tableWidget_itemClicked(QTableWidgetItem *item)
{
    giconUrl = item->data(Qt::UserRole).toString();
    if(giconUrl == "addother.png"){
        QString filepath = QFileDialog::getOpenFileName(this,"选择一个群聊图标","../test","*.png");
        giconUrl = filepath;
        if(giconUrl.isEmpty()){
            giconUrl = "earth.png";
            defaultIcon = "1";  // 若路径为空，则使用默认图标
        }else
            defaultIcon = "0";  // 若不为空，则使用自定义的群图标

         ui->lab_show->setPixmap(QPixmap(giconUrl));
    }
    else{
         ui->lab_show->setPixmap(QPixmap(QString(":/prefix2/software/%1").arg(giconUrl)));
    }
}



void CreateGroupWindow::on_btn_close_clicked()
{
    this->close();
}


void CreateGroupWindow::on_btn_min_clicked()
{
    this->showMinimized();
}

